package com.cloud.ui;

import com.cloud.DevelopProperties;
import com.cloud.bean.ServiceInfo;
import com.cloud.client.NextGISApiClient;
import com.supermap.desktop.controls.ui.controls.SmTextFieldSearch;
import com.supermap.desktop.controls.ui.controls.labels.SmLinkLabel;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.BrowserUtilities;
import com.supermap.desktop.core.utilties.FontUtilities;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ControlQuickMapService extends JPanel {
    private JPanel searchPanel;
    private SmTextFieldSearch textFieldSearch;
    private ServicePanel servicePanel;
    private JPanel tipPanel;
    private JLabel labelTip;
    private JScrollPane jScrollPane;
    private JPanel NoResultPanel;
    private JPanel aboutPanel;
    private JCheckBox checkBoxAutoOpen;
    private final NextGISApiClient client = new NextGISApiClient();
    private static boolean autoOpen;

    public ControlQuickMapService() {
        initComponents();
        initLayout();
        registerEvents();
        initState();
    }

    private void initComponents() {
        searchPanel = new JPanel();
        textFieldSearch = new SmTextFieldSearch();
        servicePanel = new ServicePanel();
        jScrollPane = new JScrollPane(servicePanel);

        tipPanel = new JPanel();
        labelTip = new JLabel();
        NoResultPanel = new JPanel();
        JLabel labelNoResult = new JLabel(DevelopProperties.getString("String_NoResult"));
        labelNoResult.setHorizontalAlignment(JLabel.CENTER);
        labelNoResult.setFont(new Font(FontUtilities.getDefaultFont().getFamily(), Font.PLAIN, FontUtilities.getDefaultFont().getSize() + 5));
        JLabel labelNoResultTip = new JLabel(DevelopProperties.getString("String_ClickToSearch"));
        labelNoResultTip.setHorizontalAlignment(JLabel.RIGHT);
        SmLinkLabel smLinkLabel = new SmLinkLabel(DevelopProperties.getString("String_Here"));
        smLinkLabel.addActionListener(e -> BrowserUtilities.openUrl(DevelopProperties.getString("String_CREATE_URL")));
        smLinkLabel.setHorizontalAlignment(JLabel.LEFT);
        NoResultPanel.setLayout(new GridBagLayout());
        NoResultPanel.add(labelNoResult, new GridBagConstraintsHelper(0, 0, 2, 1)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0));
        NoResultPanel.add(labelNoResultTip, new GridBagConstraintsHelper(0, 1, 1, 1)
                .setAnchor(GridBagConstraints.EAST)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0));
        NoResultPanel.add(smLinkLabel, new GridBagConstraintsHelper(1, 1, 1, 1)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0));

        aboutPanel = new JPanel();
        checkBoxAutoOpen = new JCheckBox(DevelopProperties.getString("String_OpenAfterAdded"));
        aboutPanel.setLayout(new GridBagLayout());
        aboutPanel.setBackground(Color.white);
        aboutPanel.add(checkBoxAutoOpen, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0));
        aboutPanel.add(new JPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setFill(GridBagConstraints.NONE)
                .setWeight(1, 0));
    }

    private void initLayout() {
        this.setLayout(new GridBagLayout());
        searchPanel.setLayout(new GridBagLayout());
        searchPanel.add(textFieldSearch, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(1, 0));
        tipPanel.setLayout(new GridBagLayout());
        tipPanel.add(labelTip, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0));

        this.add(searchPanel, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(1, 0));
        this.add(jScrollPane, new GridBagConstraintsHelper(0, 1, 1, 1)
                .setAnchor(GridBagConstraints.NORTH)
                .setFill(GridBagConstraints.BOTH)
                .setWeight(1, 1)
                .setInsets(5, 0, 0, 0));
        this.add(tipPanel, new GridBagConstraintsHelper(0, 2, 1, 1)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.BOTH)
                .setWeight(1, 1)
                .setInsets(5, 0, 0, 0));
        this.add(NoResultPanel, new GridBagConstraintsHelper(0, 3, 1, 1)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.BOTH)
                .setWeight(1, 1)
                .setInsets(5, 0, 0, 0));
        this.add(aboutPanel, new GridBagConstraintsHelper(0, 4, 1, 1)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(0, 0)
                .setInsets(5, 0, 0, 0));
    }

    private void registerEvents() {
        checkBoxAutoOpen.addItemListener(e -> autoOpen = checkBoxAutoOpen.isSelected());
        textFieldSearch.getDocument().addDocumentListener(new DocumentListener() {
            private Thread currentThread;
            private void addTask() {
                try {
                    if (currentThread != null) {
                        currentThread.stop(); // 虽然是暴力暂停，但是对于搜索来说并没有影响
                    }
                    currentThread = new Thread(new SearchTask());
                    currentThread.start();
                } catch (ThreadDeath threadDeath) {
                    // do nothing
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                addTask();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                addTask();
            }


            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
    }

    private void initState() {
        jScrollPane.setVisible(false);
        NoResultPanel.setVisible(true);
        tipPanel.setVisible(false);
        checkBoxAutoOpen.setSelected(true);
        autoOpen = true;
    }

    public static boolean isAutoOpen() {
        return autoOpen;
    }

    private class SearchTask implements Runnable {
        @Override
        public void run() {
            if (textFieldSearch.getText().length() == 0) {
                if (servicePanel.getComponentCount() > 0) {
                    NoResultPanel.setVisible(false);
                    tipPanel.setVisible(false);
                    jScrollPane.setVisible(true);
                } else {
                    NoResultPanel.setVisible(true);
                    tipPanel.setVisible(false);
                    jScrollPane.setVisible(false);
                }
                return;
            } else if (textFieldSearch.getText().length() < 3) {
                tipPanel.setVisible(true);
                jScrollPane.setVisible(false);
                NoResultPanel.setVisible(false);
                labelTip.setText(DevelopProperties.getString("String_Least3Word"));
                return;
            }
            tipPanel.setVisible(true);
            jScrollPane.setVisible(false);
            NoResultPanel.setVisible(false);
            labelTip.setText(DevelopProperties.getString("String_PlWait"));
            try {
                List<ServiceInfo> serviceInfo = client.getServiceInfo(textFieldSearch.getText());
                if (serviceInfo.size() == 0) {
                    NoResultPanel.setVisible(true);
                } else {
                    List<PanelServiceInfo> list = new ArrayList<>();
                    for (int i = 0; i < serviceInfo.size(); i++) {
                        PanelServiceInfo panel = serviceInfo.get(i).createPanel();
                        if (i % 2 != 0) {
                            panel.setBackground(Color.white);
                        }
                        list.add(panel);
                    }
                    servicePanel.setData(list);
                    jScrollPane.setVisible(true);
                }
                tipPanel.setVisible(false);
            } catch (IOException ioException) {
                jScrollPane.setVisible(false);
                tipPanel.setVisible(true);
                labelTip.setText(DevelopProperties.getString("String_NetworkRequestFailed"));
            }
        }
    }

}
